package com.onejson.ojmall.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onejson.ojmall.common.domain.LoginLogVO;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.enumeration.LoginLogResultEnum;
import com.onejson.ojmall.common.enumeration.UserTypeEnum;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SLoginLogDao;
import com.onejson.ojmall.model.entity.SLoginLogEntity;
import com.onejson.ojmall.model.form.SLoginLogBatchForm;
import com.onejson.ojmall.model.form.SLoginLogForm;
import com.onejson.ojmall.model.form.SLoginLogQueryForm;
import com.onejson.ojmall.model.vo.SLoginLogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 用户登录日志 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SLoginLogService extends ServiceImpl<SLoginLogDao, SLoginLogEntity> {

    @Resource
    private SLoginLogDao sLoginLogDao;

    /**
     * 获取用户登录日志列表
     */
    public PageResult<SLoginLogVO> getList(SLoginLogQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SLoginLogVO> list = sLoginLogDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询用户登录日志
     */
    public SLoginLogVO queryById(String detailId) {

        SLoginLogEntity detailEntity = sLoginLogDao.selectById(detailId);
        SLoginLogVO sLoginLogVO = new SLoginLogVO();

        try {
            BeanUtils.copyProperties(sLoginLogVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sLoginLogVO;

    }


    /**
     * 【用户登录日志】 - 添加
     */
    public ResponseDTO<String> addSLoginLog(SLoginLogForm addForm) {

        SLoginLogEntity entity = new SLoginLogEntity();

        addForm.setLoginLogId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sLoginLogDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【用户登录日志】 - 更新
     */
    public ResponseDTO<String> updateSLoginLog(SLoginLogForm updateForm) {

        SLoginLogEntity entity = new SLoginLogEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sLoginLogDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【用户登录日志】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSLoginLog(SLoginLogBatchForm sLoginLogBatchForm) {

        SLoginLogEntity entity = new SLoginLogEntity();
        List<SLoginLogEntity> sLoginLogEntityList = new ArrayList<>();

        try {
            for (SLoginLogForm sLoginLogForm : sLoginLogBatchForm.getSLoginLogFormList()) {

                sLoginLogForm.setCreateTime(null);
                sLoginLogForm.setUpdateTime(null);
                entity.setRemark(sLoginLogBatchForm.getRemark());
                BeanUtils.copyProperties(entity, sLoginLogForm);
                sLoginLogEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sLoginLogEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sLoginLogDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sLoginLogDao.deleteById(id);
        return ResponseDTO.ok();
    }


    /**
     * 查询上一个登录记录
     *
     *
     * @description 查询上一个登录记录
     */
    public LoginLogVO queryLastByUserId(Long userId, UserTypeEnum userTypeEnum, LoginLogResultEnum loginLogResultEnum) {
        return sLoginLogDao.queryLastByUserId(userId,userTypeEnum.getValue(), loginLogResultEnum.getValue());
    }

}

